{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "63c8aad1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import tushare as ts\n",
    "import os\n",
    "from datetime import datetime\n",
    "import time\n",
    "from tqdm.notebook import tqdm\n",
    "from utils import get_adj_data\n",
    "from layers import *\n",
    "\n",
    "token = 'd942e6ff0e981f76aaa544f84405583e0c2129a8c82213637835a099'\n",
    "pro = ts.pro_api(token)\n",
    "\n",
    "special_index = \\\n",
    "[1,  2,  3,  4,  5,  6,  7,  8, 11, 12, \n",
    " 13, 14, 15, 16, 17, 21, 22, 23,24, 25, \n",
    " 26, 31, 32, 33, 34, 35, 41, 42, 43, 44,\n",
    " 51, 52, 53, 61, 62, 71]\n",
    "\n",
    "feature_names = ['open','high','low','close','vwap','volume','return1','turn','free_turn']\n",
    "\n",
    "pred_n = 5 # 预测未来n天的收益率\n",
    "\n",
    "dd_calender = pro.trade_cal(exchange='SSE')\n",
    "dd_calender = dd_calender[dd_calender['is_open']==1]\n",
    "dd_calender = dd_calender.sort_values(by=['cal_date'])\n",
    "dd_calender.index = pd.to_datetime(dd_calender['cal_date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2ad4ed7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "folder = '基础数据/'\n",
    "files = os.listdir(folder)\n",
    "\n",
    "file = '601012.SH.csv'\n",
    "\n",
    "df = pd.read_csv(folder+file,index_col=0).sort_index()\n",
    "df.index = pd.to_datetime(df.index)\n",
    "df = df[-230:]\n",
    "\n",
    "df = pd.concat([dd_calender,df],axis=1).sort_index()\n",
    "# 剔除下一交易日停牌的日期\n",
    "df = df[df.index.isin(df.shift(-1).dropna().index)].dropna()\n",
    "df['ret'] = (df['close'].shift(-pred_n) - df['close'])/df['close']\n",
    "df = df.dropna()\n",
    "y = df['ret']\n",
    "df = df[feature_names]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ccfb7200",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>vwap</th>\n",
       "      <th>volume</th>\n",
       "      <th>return1</th>\n",
       "      <th>turn</th>\n",
       "      <th>free_turn</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-09-23</th>\n",
       "      <td>50.542752</td>\n",
       "      <td>54.722071</td>\n",
       "      <td>49.682722</td>\n",
       "      <td>54.722071</td>\n",
       "      <td>73.558185</td>\n",
       "      <td>808677.91</td>\n",
       "      <td>0.095323</td>\n",
       "      <td>2.1464</td>\n",
       "      <td>3.2549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-24</th>\n",
       "      <td>53.940226</td>\n",
       "      <td>54.302718</td>\n",
       "      <td>49.476599</td>\n",
       "      <td>49.533461</td>\n",
       "      <td>72.206569</td>\n",
       "      <td>957772.68</td>\n",
       "      <td>-0.099619</td>\n",
       "      <td>2.5422</td>\n",
       "      <td>3.8550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-25</th>\n",
       "      <td>50.436137</td>\n",
       "      <td>51.900320</td>\n",
       "      <td>48.865338</td>\n",
       "      <td>50.549860</td>\n",
       "      <td>71.274419</td>\n",
       "      <td>540741.75</td>\n",
       "      <td>0.020312</td>\n",
       "      <td>1.4353</td>\n",
       "      <td>2.1765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-28</th>\n",
       "      <td>51.658659</td>\n",
       "      <td>54.018410</td>\n",
       "      <td>51.281951</td>\n",
       "      <td>52.596873</td>\n",
       "      <td>74.108012</td>\n",
       "      <td>600078.77</td>\n",
       "      <td>0.039697</td>\n",
       "      <td>1.5928</td>\n",
       "      <td>2.4153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-09-29</th>\n",
       "      <td>53.378719</td>\n",
       "      <td>53.947333</td>\n",
       "      <td>52.106443</td>\n",
       "      <td>53.371611</td>\n",
       "      <td>74.896973</td>\n",
       "      <td>410293.27</td>\n",
       "      <td>0.014622</td>\n",
       "      <td>1.0890</td>\n",
       "      <td>1.6514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-19</th>\n",
       "      <td>81.900000</td>\n",
       "      <td>82.800000</td>\n",
       "      <td>77.110000</td>\n",
       "      <td>80.390000</td>\n",
       "      <td>79.706172</td>\n",
       "      <td>1242399.96</td>\n",
       "      <td>-0.019586</td>\n",
       "      <td>2.2953</td>\n",
       "      <td>2.9135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-20</th>\n",
       "      <td>79.590000</td>\n",
       "      <td>81.400000</td>\n",
       "      <td>78.280000</td>\n",
       "      <td>79.120000</td>\n",
       "      <td>79.580449</td>\n",
       "      <td>704668.82</td>\n",
       "      <td>-0.015924</td>\n",
       "      <td>1.3018</td>\n",
       "      <td>1.6525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-23</th>\n",
       "      <td>79.550000</td>\n",
       "      <td>81.200000</td>\n",
       "      <td>77.500000</td>\n",
       "      <td>80.750000</td>\n",
       "      <td>79.520670</td>\n",
       "      <td>830602.39</td>\n",
       "      <td>0.020392</td>\n",
       "      <td>1.5345</td>\n",
       "      <td>1.9478</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-24</th>\n",
       "      <td>81.550000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>80.660000</td>\n",
       "      <td>84.140000</td>\n",
       "      <td>83.989237</td>\n",
       "      <td>1196427.34</td>\n",
       "      <td>0.041124</td>\n",
       "      <td>2.2104</td>\n",
       "      <td>2.8057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-08-25</th>\n",
       "      <td>85.010000</td>\n",
       "      <td>88.880000</td>\n",
       "      <td>84.290000</td>\n",
       "      <td>88.130000</td>\n",
       "      <td>86.758749</td>\n",
       "      <td>1294191.33</td>\n",
       "      <td>0.046331</td>\n",
       "      <td>2.3910</td>\n",
       "      <td>3.0349</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>224 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 open       high        low      close       vwap      volume  \\\n",
       "2020-09-23  50.542752  54.722071  49.682722  54.722071  73.558185   808677.91   \n",
       "2020-09-24  53.940226  54.302718  49.476599  49.533461  72.206569   957772.68   \n",
       "2020-09-25  50.436137  51.900320  48.865338  50.549860  71.274419   540741.75   \n",
       "2020-09-28  51.658659  54.018410  51.281951  52.596873  74.108012   600078.77   \n",
       "2020-09-29  53.378719  53.947333  52.106443  53.371611  74.896973   410293.27   \n",
       "...               ...        ...        ...        ...        ...         ...   \n",
       "2021-08-19  81.900000  82.800000  77.110000  80.390000  79.706172  1242399.96   \n",
       "2021-08-20  79.590000  81.400000  78.280000  79.120000  79.580449   704668.82   \n",
       "2021-08-23  79.550000  81.200000  77.500000  80.750000  79.520670   830602.39   \n",
       "2021-08-24  81.550000  86.000000  80.660000  84.140000  83.989237  1196427.34   \n",
       "2021-08-25  85.010000  88.880000  84.290000  88.130000  86.758749  1294191.33   \n",
       "\n",
       "             return1    turn  free_turn  \n",
       "2020-09-23  0.095323  2.1464     3.2549  \n",
       "2020-09-24 -0.099619  2.5422     3.8550  \n",
       "2020-09-25  0.020312  1.4353     2.1765  \n",
       "2020-09-28  0.039697  1.5928     2.4153  \n",
       "2020-09-29  0.014622  1.0890     1.6514  \n",
       "...              ...     ...        ...  \n",
       "2021-08-19 -0.019586  2.2953     2.9135  \n",
       "2021-08-20 -0.015924  1.3018     1.6525  \n",
       "2021-08-23  0.020392  1.5345     1.9478  \n",
       "2021-08-24  0.041124  2.2104     2.8057  \n",
       "2021-08-25  0.046331  2.3910     3.0349  \n",
       "\n",
       "[224 rows x 9 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5d9b00c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2a6fd0b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "d1 = 10\n",
    "d2 = 3\n",
    "days = 30\n",
    "XX = df.values\n",
    "XX = torch.cat([torch.tensor(XX[i-days:i]).to(torch.float32).unsqueeze(0) \n",
    "                for i in range(days,len(XX))])\n",
    "yy = torch.tensor(y[days:]).to(torch.float32)\n",
    "\n",
    "XX_train = XX[:int(len(XX)/2)]\n",
    "XX_test = XX[int(len(XX)/2):]\n",
    "\n",
    "yy_train = yy[:int(len(yy)/2)]\n",
    "yy_test = yy[int(len(yy)/2):]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c68ceee2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.0257,  0.1766,  0.1632,  0.1174,  0.0630,  0.0385, -0.0693, -0.1357,\n",
       "        -0.1161, -0.0980, -0.0950, -0.1097, -0.0140, -0.0213,  0.0049,  0.0553,\n",
       "         0.1213,  0.0486, -0.0053, -0.0327, -0.0028, -0.0613, -0.0212,  0.0540,\n",
       "         0.0458, -0.0533, -0.0669, -0.0734, -0.1284, -0.1405, -0.0745,  0.0050,\n",
       "         0.0124,  0.0259,  0.0644,  0.0407,  0.0390,  0.0148,  0.0455,  0.0440,\n",
       "         0.0290, -0.0037,  0.0440,  0.0375,  0.0524,  0.0673,  0.0514,  0.0565,\n",
       "         0.0520,  0.0358,  0.0096,  0.0666,  0.1274,  0.1519,  0.2132,  0.2111,\n",
       "         0.2035,  0.0788,  0.0057, -0.0158,  0.0177,  0.0712,  0.1380,  0.2104,\n",
       "         0.2514,  0.1974,  0.0604,  0.0484,  0.0110, -0.1106, -0.0729, -0.0281,\n",
       "        -0.0678,  0.0012,  0.1290,  0.1695,  0.1318,  0.1130,  0.1356, -0.0068,\n",
       "        -0.1015, -0.0844, -0.0444, -0.1153, -0.0221,  0.0657,  0.0494,  0.0771,\n",
       "         0.1209,  0.1044,  0.0208, -0.0296, -0.0523, -0.1474, -0.1496, -0.1072,\n",
       "         0.0049])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yy_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e5f54dd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "39e369ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        d1 = 10\n",
    "        d2 = 3\n",
    "        \n",
    "        self.layer1 = ts_corr(d1)\n",
    "        self.bn_layer1 = nn.BatchNorm1d(1800)\n",
    "\n",
    "        self.layer2 = ts_stddev(d1)\n",
    "        self.bn_layer2 = nn.BatchNorm1d(180)\n",
    "\n",
    "        self.layer3 = ts_zscore(d1)\n",
    "        self.bn_layer3 = nn.BatchNorm1d(180)\n",
    "\n",
    "        self.layer4 = ts_return(d1)\n",
    "        self.bn_layer4 = nn.BatchNorm1d(180)\n",
    "\n",
    "        self.layer5 = ts_decaylinear(d1)\n",
    "        self.bn_layer5 = nn.BatchNorm1d(180)\n",
    "\n",
    "        self.layer6 = ts_mean(d1)\n",
    "        self.bn_layer6 = nn.BatchNorm1d(180)\n",
    "\n",
    "        self.bn_layer11 = nn.BatchNorm1d(2700)\n",
    "        self.bn_layer12 = nn.BatchNorm1d(2700)\n",
    "        self.bn_layer13 = nn.BatchNorm1d(2700)\n",
    "        \n",
    "        self.fc1 = nn.Linear(8100,30)\n",
    "        self.fc2 = nn.Linear(30,1)\n",
    "        self.dropout = nn.Dropout(0.5)\n",
    "        self.relu = nn.ReLU()\n",
    "\n",
    "    def forward(self, x):\n",
    "        x1 = self.layer1(x)\n",
    "        x1 = self.bn_layer1(x1)\n",
    "\n",
    "        x2 = self.layer2(x)\n",
    "        x2 = self.bn_layer2(x2)\n",
    "\n",
    "        x3 = self.layer3(x)\n",
    "        x3 = self.bn_layer3(x3)\n",
    "\n",
    "        x4 = self.layer4(x)\n",
    "        x4 = self.bn_layer4(x4)\n",
    "\n",
    "        x5 = self.layer5(x)\n",
    "        x5 = self.bn_layer5(x5)\n",
    "\n",
    "        x6 = self.layer6(x)\n",
    "        x6 = self.bn_layer6(x6)\n",
    "\n",
    "        xx = torch.cat([x1,x2,x3,x4,x5,x6],1)\n",
    "\n",
    "        xx1 = torch.cat([xx[:,i:i+d2].mean(1).unsqueeze(1) for i in range(xx.shape[1])],1)\n",
    "        xx2 = torch.cat([xx[:,i:i+d2].max(1).values.unsqueeze(1) for i in range(xx.shape[1])],1)\n",
    "        xx3 = torch.cat([xx[:,i:i+d2].min(1).values.unsqueeze(1) for i in range(xx.shape[1])],1)\n",
    "\n",
    "        xx1 = self.bn_layer13(xx1)\n",
    "        xx2 = self.bn_layer12(xx2)\n",
    "        xx3 = self.bn_layer13(xx3)\n",
    "\n",
    "        xx = torch.cat([xx1,xx2,xx3],1)\n",
    "        \n",
    "        xx = self.fc1(xx)\n",
    "        xx = self.relu(xx)\n",
    "        xx = self.dropout(xx)\n",
    "        xx = self.fc2(xx)\n",
    "        return xx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7d7b4cbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = Net()\n",
    "optimizer = optim.Adam(model.parameters(), lr=0.002)\n",
    "loss_func = nn.MSELoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "517d9eb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([97, 30, 9])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "XX_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "11e0e973",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([97, 180])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.layer2(XX_train).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f7b92b68",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = XX_train\n",
    "\n",
    "x1 = model.layer1(x)\n",
    "x1 = model.bn_layer1(x1)\n",
    "\n",
    "x2 = model.layer2(x)\n",
    "x2 = model.bn_layer2(x2)\n",
    "\n",
    "x3 = model.layer3(x)\n",
    "x3 = model.bn_layer3(x3)\n",
    "\n",
    "x4 = model.layer4(x)\n",
    "x4 = model.bn_layer4(x4)\n",
    "\n",
    "x5 = model.layer5(x)\n",
    "x5 = model.bn_layer5(x5)\n",
    "\n",
    "x6 = model.layer6(x)\n",
    "x6 = model.bn_layer6(x6)\n",
    "\n",
    "xx = torch.cat([x1,x2,x3,x4,x5,x6],1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "4208f7c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 5.0543e+01,  5.4722e+01,  4.9683e+01,  ...,  9.5323e-02,\n",
       "           2.1464e+00,  3.2549e+00],\n",
       "         [ 5.3940e+01,  5.4303e+01,  4.9477e+01,  ..., -9.9619e-02,\n",
       "           2.5422e+00,  3.8550e+00],\n",
       "         [ 5.0436e+01,  5.1900e+01,  4.8865e+01,  ...,  2.0312e-02,\n",
       "           1.4353e+00,  2.1765e+00],\n",
       "         ...,\n",
       "         [ 5.0920e+01,  5.2887e+01,  4.9766e+01,  ...,  1.1707e-02,\n",
       "           1.9477e+00,  2.7893e+00],\n",
       "         [ 5.1597e+01,  5.4227e+01,  5.0122e+01,  ...,  3.5660e-02,\n",
       "           1.9495e+00,  2.7918e+00],\n",
       "         [ 5.2737e+01,  5.4804e+01,  5.2025e+01,  ..., -2.4225e-02,\n",
       "           1.6911e+00,  2.4217e+00]],\n",
       "\n",
       "        [[ 5.3940e+01,  5.4303e+01,  4.9477e+01,  ..., -9.9619e-02,\n",
       "           2.5422e+00,  3.8550e+00],\n",
       "         [ 5.0436e+01,  5.1900e+01,  4.8865e+01,  ...,  2.0312e-02,\n",
       "           1.4353e+00,  2.1765e+00],\n",
       "         [ 5.1659e+01,  5.4018e+01,  5.1282e+01,  ...,  3.9697e-02,\n",
       "           1.5928e+00,  2.4153e+00],\n",
       "         ...,\n",
       "         [ 5.1597e+01,  5.4227e+01,  5.0122e+01,  ...,  3.5660e-02,\n",
       "           1.9495e+00,  2.7918e+00],\n",
       "         [ 5.2737e+01,  5.4804e+01,  5.2025e+01,  ..., -2.4225e-02,\n",
       "           1.6911e+00,  2.4217e+00],\n",
       "         [ 5.3158e+01,  5.3308e+01,  4.9744e+01,  ..., -4.1250e-02,\n",
       "           2.3280e+00,  3.3338e+00]],\n",
       "\n",
       "        [[ 5.0436e+01,  5.1900e+01,  4.8865e+01,  ...,  2.0312e-02,\n",
       "           1.4353e+00,  2.1765e+00],\n",
       "         [ 5.1659e+01,  5.4018e+01,  5.1282e+01,  ...,  3.9697e-02,\n",
       "           1.5928e+00,  2.4153e+00],\n",
       "         [ 5.3379e+01,  5.3947e+01,  5.2106e+01,  ...,  1.4622e-02,\n",
       "           1.0890e+00,  1.6514e+00],\n",
       "         ...,\n",
       "         [ 5.2737e+01,  5.4804e+01,  5.2025e+01,  ..., -2.4225e-02,\n",
       "           1.6911e+00,  2.4217e+00],\n",
       "         [ 5.3158e+01,  5.3308e+01,  4.9744e+01,  ..., -4.1250e-02,\n",
       "           2.3280e+00,  3.3338e+00],\n",
       "         [ 4.9937e+01,  5.0022e+01,  4.5661e+01,  ..., -5.1105e-02,\n",
       "           3.7745e+00,  5.4054e+00]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[ 8.8642e+01,  8.9568e+01,  8.5527e+01,  ...,  2.6199e-03,\n",
       "           2.0651e+00,  2.6392e+00],\n",
       "         [ 8.5356e+01,  8.6019e+01,  8.1030e+01,  ..., -4.4303e-02,\n",
       "           2.0621e+00,  2.6354e+00],\n",
       "         [ 8.1957e+01,  8.5093e+01,  7.7324e+01,  ..., -7.1928e-02,\n",
       "           2.3532e+00,  3.0073e+00],\n",
       "         ...,\n",
       "         [ 5.7313e+01,  5.8061e+01,  5.6230e+01,  ..., -2.7394e-03,\n",
       "           1.5309e+00,  1.9564e+00],\n",
       "         [ 5.7149e+01,  6.2779e+01,  5.6728e+01,  ...,  8.8486e-02,\n",
       "           2.7696e+00,  3.5395e+00],\n",
       "         [ 6.2708e+01,  6.4618e+01,  6.1525e+01,  ...,  4.3275e-03,\n",
       "           2.1524e+00,  2.7507e+00]],\n",
       "\n",
       "        [[ 8.5356e+01,  8.6019e+01,  8.1030e+01,  ..., -4.4303e-02,\n",
       "           2.0621e+00,  2.6354e+00],\n",
       "         [ 8.1957e+01,  8.5093e+01,  7.7324e+01,  ..., -7.1928e-02,\n",
       "           2.3532e+00,  3.0073e+00],\n",
       "         [ 7.5543e+01,  8.0923e+01,  7.4474e+01,  ...,  2.1713e-02,\n",
       "           1.7376e+00,  2.2205e+00],\n",
       "         ...,\n",
       "         [ 5.7149e+01,  6.2779e+01,  5.6728e+01,  ...,  8.8486e-02,\n",
       "           2.7696e+00,  3.5395e+00],\n",
       "         [ 6.2708e+01,  6.4618e+01,  6.1525e+01,  ...,  4.3275e-03,\n",
       "           2.1524e+00,  2.7507e+00],\n",
       "         [ 6.3606e+01,  6.6100e+01,  6.2487e+01,  ...,  1.8242e-02,\n",
       "           1.6910e+00,  2.1464e+00]],\n",
       "\n",
       "        [[ 8.1957e+01,  8.5093e+01,  7.7324e+01,  ..., -7.1928e-02,\n",
       "           2.3532e+00,  3.0073e+00],\n",
       "         [ 7.5543e+01,  8.0923e+01,  7.4474e+01,  ...,  2.1713e-02,\n",
       "           1.7376e+00,  2.2205e+00],\n",
       "         [ 7.8386e+01,  7.9527e+01,  7.1987e+01,  ..., -6.7569e-02,\n",
       "           2.6650e+00,  3.4058e+00],\n",
       "         ...,\n",
       "         [ 6.2708e+01,  6.4618e+01,  6.1525e+01,  ...,  4.3275e-03,\n",
       "           2.1524e+00,  2.7507e+00],\n",
       "         [ 6.3606e+01,  6.6100e+01,  6.2487e+01,  ...,  1.8242e-02,\n",
       "           1.6910e+00,  2.1464e+00],\n",
       "         [ 6.3698e+01,  6.4361e+01,  6.1774e+01,  ..., -8.1789e-03,\n",
       "           1.4590e+00,  1.8520e+00]]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78507bcf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c1e53a2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e68c49b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "fe553fac",
   "metadata": {},
   "outputs": [],
   "source": [
    "BATCH_SIZE = 10\n",
    "torch_data  = Data.TensorDataset(XX_train, yy_train)\n",
    "loader = Data.DataLoader(dataset=torch_data, batch_size=BATCH_SIZE, shuffle=False)\n",
    "\n",
    "BATCH_SIZE = 10\n",
    "torch_data  = Data.TensorDataset(XX_test, yy_test)\n",
    "test_loader = Data.DataLoader(dataset=torch_data, batch_size=BATCH_SIZE, shuffle=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c71b6cc1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "efd95690d62e459eb1a43ef91758bbf2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/10 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 nan\n",
      "1 nan\n",
      "2 nan\n",
      "3 nan\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-31-03e279ca4de2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      7\u001b[0m         \u001b[0mloss\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mloss_func\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpred\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb_y\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m         \u001b[0moptimizer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mzero_grad\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m         \u001b[0mloss\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     10\u001b[0m         \u001b[0moptimizer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m         \u001b[0msum_loss\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mloss\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitem\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\torch\\_tensor.py\u001b[0m in \u001b[0;36mbackward\u001b[1;34m(self, gradient, retain_graph, create_graph, inputs)\u001b[0m\n\u001b[0;32m    253\u001b[0m                 \u001b[0mcreate_graph\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcreate_graph\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    254\u001b[0m                 inputs=inputs)\n\u001b[1;32m--> 255\u001b[1;33m         \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mautograd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgradient\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mretain_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0minputs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    256\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    257\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mregister_hook\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhook\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\lib\\site-packages\\torch\\autograd\\__init__.py\u001b[0m in \u001b[0;36mbackward\u001b[1;34m(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)\u001b[0m\n\u001b[0;32m    145\u001b[0m         \u001b[0mretain_graph\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    146\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 147\u001b[1;33m     Variable._execution_engine.run_backward(\n\u001b[0m\u001b[0;32m    148\u001b[0m         \u001b[0mtensors\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgrad_tensors_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mretain_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    149\u001b[0m         allow_unreachable=True, accumulate_grad=True)  # allow_unreachable flag\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "model.train()\n",
    "for t in tqdm(range(10)):\n",
    "    sum_loss = 0\n",
    "    for step, (b_x, b_y) in enumerate(loader):\n",
    "        pred = model(b_x)\n",
    "        pred = pred.squeeze()\n",
    "        loss = loss_func(pred,b_y)\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        sum_loss += loss.item()\n",
    "    print(t,sum_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "cdcf300f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[nan],\n",
       "        [nan],\n",
       "        [nan],\n",
       "        [nan],\n",
       "        [nan],\n",
       "        [nan],\n",
       "        [nan],\n",
       "        [nan],\n",
       "        [nan],\n",
       "        [nan]], grad_fn=<AddmmBackward>)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model(b_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "663edfce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.0096,  0.0666,  0.1274,  0.1519,  0.2132,  0.2111,  0.2035,  0.0788,\n",
       "         0.0057, -0.0158])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8160e72",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25181249",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee25c277",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "192008be",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "342d8a9a2ff34ae19a182917d35463b2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(HTML(value=''), FloatProgress(value=1.0, bar_style='info', layout=Layout(width='20px'), max=1.0…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "9 0\n"
     ]
    }
   ],
   "source": [
    "model.eval()\n",
    "sum_loss = 0\n",
    "preds = []\n",
    "for step, (b_x, b_y) in tqdm(enumerate(test_loader)):\n",
    "    pred = model(b_x)\n",
    "    pred = pred.squeeze()\n",
    "    preds = preds + list(pred.detach().numpy())\n",
    "print(t,sum_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "954ec814",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.44329896907216493"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((yy_test.detach().numpy()>0) == (np.array(preds)>0)).sum()/len(preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a36b1535",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fc563c5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d19ba84b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4414ce2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5e33687",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6bd8c12",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d642c6e5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d5919026",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.4763],\n",
       "        [-0.0431]], grad_fn=<AddmmBackward>)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = Net()\n",
    "model(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d16ed270",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "15dcdcb0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "layer1 = ts_corr(d1)\n",
    "bn_layer1 = nn.BatchNorm1d(1800)\n",
    "\n",
    "layer2 = ts_stddev(d1)\n",
    "bn_layer2 = nn.BatchNorm1d(180)\n",
    "\n",
    "layer3 = ts_zscore(d1)\n",
    "bn_layer3 = nn.BatchNorm1d(180)\n",
    "\n",
    "layer4 = ts_return(d1)\n",
    "bn_layer4 = nn.BatchNorm1d(180)\n",
    "\n",
    "layer5 = ts_decaylinear(d1)\n",
    "bn_layer5 = nn.BatchNorm1d(180)\n",
    "\n",
    "layer6 = ts_mean(d1)\n",
    "bn_layer6 = nn.BatchNorm1d(180)\n",
    "\n",
    "bn_layer11 = nn.BatchNorm1d(10800)\n",
    "bn_layer12 = nn.BatchNorm1d(10800)\n",
    "bn_layer13 = nn.BatchNorm1d(10800)\n",
    "\n",
    "x1 = layer1(x)\n",
    "x1 = bn_layer1(x1)\n",
    "\n",
    "x2 = layer1(x)\n",
    "x2 = bn_layer1(x2)\n",
    "\n",
    "x3 = layer1(x)\n",
    "x3 = bn_layer1(x3)\n",
    "\n",
    "x4 = layer1(x)\n",
    "x4 = bn_layer1(x4)\n",
    "\n",
    "x5 = layer1(x)\n",
    "x5 = bn_layer1(x5)\n",
    "\n",
    "x6 = layer1(x)\n",
    "x6 = bn_layer1(x6)\n",
    "\n",
    "\n",
    "xx = torch.cat([x1,x2,x3,x4,x5,x6],1)\n",
    "\n",
    "xx1 = torch.cat([xx[:,i:i+d2].mean(1).unsqueeze(1) for i in range(xx.shape[1])],1)\n",
    "xx2 = torch.cat([xx[:,i:i+d2].max(1).values.unsqueeze(1) for i in range(xx.shape[1])],1)\n",
    "xx3 = torch.cat([xx[:,i:i+d2].min(1).values.unsqueeze(1) for i in range(xx.shape[1])],1)\n",
    "\n",
    "xx1 = bn_layer13(xx1)\n",
    "xx2 = bn_layer12(xx2)\n",
    "xx3 = bn_layer13(xx3)\n",
    "\n",
    "xx = torch.cat([xx1,xx2,xx3],1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "d356409c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.0000, -1.0000, -1.0000,  ...,  0.9775,  1.0000,  1.0000],\n",
       "        [ 1.0000,  1.0000,  1.0000,  ..., -0.9775, -1.0000, -1.0000]],\n",
       "       grad_fn=<CatBackward>)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "c1f10124",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 10800])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b04a5440",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.9965, -0.9988, -0.9993, -0.9946, -0.9997,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000, -0.9957,  0.0000, -0.9966,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000, -0.9790, -0.7683, -0.9976, -0.9985,  1.0000,\n",
       "          0.0000,  0.9975,  0.9997, -0.9993, -0.9993, -0.9993, -0.9997, -0.9997,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000, -0.9998, -0.9997, -0.9997, -0.9996,\n",
       "         -0.9998,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000, -0.7683,  0.9231,\n",
       "          0.9982,  0.9980,  0.0000,  0.0000,  0.0000,  0.0000,  0.9864,  0.9231,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.9231,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.9864,  0.0000,  0.0000,  0.0000,  0.0000, -0.9379,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000, -1.0000,  0.0000, -0.9955, -0.9994,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000, -0.9790, -0.9864, -0.9373,  0.0000, -0.3671,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000, -0.7683,  0.0000, -0.5044,  1.0000,  0.0000,  0.9966,\n",
       "          0.9996,  0.0000,  0.0000,  0.0000, -0.9905,  0.0000,  1.0000,  0.0000,\n",
       "          0.9986,  0.9998, -0.9965, -0.9635,  0.9864,  0.9930,  0.9895,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.9905,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          1.0000,  0.0000,  0.9947,  0.9993],\n",
       "        [ 0.9966,  0.9988,  0.9993,  0.9946,  0.9997,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.9958,  0.0000,  0.9965,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.9790,  0.7683,  0.9976,  0.9985, -1.0000,\n",
       "          0.0000, -0.9975, -0.9997,  0.9993,  0.9993,  0.9993,  0.9997,  0.9997,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.9998,  0.9997,  0.9997,  0.9996,\n",
       "          0.9998,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.7683, -0.9231,\n",
       "         -0.9982, -0.9980,  0.0000,  0.0000,  0.0000,  0.0000, -0.9864, -0.9231,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000, -0.9231,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000, -0.9864,  0.0000,  0.0000,  0.0000,  0.0000,  0.9379,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  1.0000,  0.0000,  0.9955,  0.9994,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "          0.0000,  0.9790,  0.9864,  0.9373,  0.0000,  0.3671,  0.0000,  0.0000,\n",
       "          0.0000,  0.0000,  0.7683,  0.0000,  0.5045, -1.0000,  0.0000, -0.9966,\n",
       "         -0.9996,  0.0000,  0.0000,  0.0000,  0.9905,  0.0000, -1.0000,  0.0000,\n",
       "         -0.9986, -0.9998,  0.9966,  0.9635, -0.9864, -0.9930, -0.9895,  0.0000,\n",
       "          0.0000,  0.0000,  0.0000, -0.9905,  0.0000,  0.0000,  0.0000,  0.0000,\n",
       "         -1.0000,  0.0000, -0.9947, -0.9993]],\n",
       "       grad_fn=<NativeBatchNormBackward>)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bn_layer = nn.BatchNorm1d(x.shape[1])\n",
    "bn_layer(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7280075d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 30, 9])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e077400",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
